package com.jinbiao.Transaction;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * 
 * @author EX_WLJR_ZHUANGJB
 *
 */
public class Transaction_Template implements Transaction_Dao {

	private DataSource ds;
	private JdbcTemplate jdbcTemplate;
	private PlatformTransactionManager manager;
	public void setDs(DataSource ds) {
		this.ds = ds;
		this.jdbcTemplate = new JdbcTemplate(ds);
		
	}
	
	 public void setManager(
		      PlatformTransactionManager manager) {
		      this.manager = manager;
		   }

	 
	public void create(String name, Integer age, Integer marks, Integer year) {
		TransactionDefinition def =  new DefaultTransactionDefinition();
		TransactionStatus status =  manager.getTransaction(def);
		
		try {
			String SQL1 ="insert into student(name,age) values (?,?)";
			jdbcTemplate.update(SQL1, name,age);

			//查询最大的id student
			//String SQL2 ="selcet max(id) from student";
			
			// int sid = jdbcTemplate.queryForObject( SQL2,Integer.class );
			
			// String SQL3 ="insert into marks(sid,marks,year) values (?,?,?)";
			// jdbcTemplate.update(SQL3,sid,marks,year);
			 System.out.println("开始创建记录");
			 
			//这个地方添加异常信息    不会提交事务 
			 double a =  2/0;
			 
			//提交事务
			 manager.commit(status);
	
			 System.out.println(status.isCompleted());
		} catch (Exception e) {
			System.out.println("error is coming");
			manager.rollback(status);
			System.out.println(status.isCompleted());
		}
		
		
	}

	public List<StudentsMarks> listStudent() {
		// TODO Auto-generated method stub
		return null;
	}
	
}


